//globals
var enabledVideos;


$(function() {
	//Get array of videos with two-screen content
	getEnabledVids();
	
});

function create(title, vidid, pic) {
	document.getElementById("form-video-id").value = vidid;
	document.getElementById("form-video-title").value = title;
	document.getElementById("form-video-img").value = pic;
	document.forms["select-video"].submit();
}

function getEnabledVids() {
	dataString = "code=3&viewer=" + viewerId;
	$.ajax({
		type: "GET",
		url: "/event",
		data: dataString,
		success: function(data) {
			enabledVideos = data;
			//Get the YT authorisation code and send to update wall
			requestYtWallData();
		}
	});
}

function requestYtWallData() {
	if (ytc != null) {
		//Request the video wall based on the YouTube code passed from a refresh token 
			//This happens when a user has just registered to become a producer - the data store is not fast enough to save
		dataString = 'code=1&ytc=' + ytc + "&viewer=" + viewerId;
		$.ajax({
			type: "GET",
			url: "/authorise",
			data: dataString,
			success: function(data) {
				status = data.status;
				message = data.message;
				array = data.entry;
				if (status == "ok") {
					updateWallSuccess(array);
				} else {
					updateWallFail(message);
				}
			},
			error: function(jqXHR, textStatus, errorThrown) {
				console.log("caught error");
				$('#video-wall').html("<h3 class=\"errorMessage\">Sorry, "+
				"there has been an error with the request. It has been logged - please try again later.</h3>");
				$.ajax({
					type: "POST",
					url: "/logger?code=2&status=" + textStatus,
					data: jqXHR
				});
			}
		});
		
	} else {
		//Request the video wall based on the YouTube code in the data store
		dataString = 'code=1';
		$.ajax({
			type: "GET",
			url: "/authorise",
			data: dataString,
			success: function(data) {
				status = data.status;
				message = data.message;
				array = data.entry;
				if (status == "ok") {
					updateWallSuccess(array);
				} else {
					updateWallFail(message);
				}
			},
			error: function(jqXHR, textStatus, errorThrown) {
				console.log("caught error");
				$('#video-wall').html("<h3 class=\"errorMessage\">Sorry, "+
				"there has been an error with the request. It has been logged - please try again later.</h3>");
				$.ajax({
					type: "POST",
					url: "/logger?code=2&status=" + textStatus,
					data: jqXHR
				});
			}
		});
		
	}
}

function updateWallSuccess(array) {
	appendString = "";
	for (entry in array) {
	    title = array[entry].title.$t;
	    vidid = array[entry].media$group.yt$videoid.$t;
	    pic = array[entry].media$group.media$thumbnail[1].url;
	    match = false;
	    
	    for (v in enabledVideos.videoID) {
	    	vid = enabledVideos.videoID[v];
	    	if (vid == vidid) {
	    		match = true;
	    	}
	    }
	    
	    $('#video-wall').html("");
	    
	    appendString += '<div class="third-video-wall">' +
			'<img onclick="create(\'' + title + '\', \'' + vidid + '\', \'' + pic + '\');" src="' + pic + '" alt="Video wall image" />';
		if (match == true) {
			appendString += '<img class="enabled-badge" src="/images/enabled-badge.png" title="Two-Screen content created" alt="Two-Screen created" />';
		}
		appendString += '<h3 class="video-wall-title" onclick="create(\'' + title + '\', \'' + vidid + '\', \'' + pic + '\');">' + 	title + '</h3>' + '</div>';
	}
	if (appendString.length == 0) {
		$('#video-wall').html("");
		appendString = '<h3>No embeddable video</h3><p>You do not have any videos uploaded to YouTube that are embeddable</p>' +
			'<p>Please go to YouTube and enable video embedding:</p>' +
				'<p>1. Go to the uploads section of your video manager and click edit on your chosen video</p>' +
				'<p>2. Click the "Advanced Settings" tab</p>' + 
				'<p>3. Under distribution options select "Allow Embedding"</p>';
	}
	
	$('#video-wall').append(appendString);
}

function updateWallFail(message) {
	$('#video-wall').html("<h3 class=\"errorMessage\">Sorry, there has been an error with the request:" +
			 message + "It has been logged - please try again later.</h3>");
}
